package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mapper.ChinaMapper;
import com.example.pojo.China;
import com.example.service.ChinaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChinaServiceImpl implements ChinaService {

    private final ChinaMapper chinaMapper;

    @Override
    public List<String> getAllProvinces() {
        LambdaQueryWrapper<China> lqw = new LambdaQueryWrapper<>();
        lqw.eq(China::getPid, 0);
        List<China> chinas = chinaMapper.selectList(lqw);
        List<String> provinces = new ArrayList<>();
        for(China china : chinas) {
            provinces.add(china.getName());
        }
        return provinces;
    }

    @Override
    public List<String> getAllCitys(String province) {
        LambdaQueryWrapper<China> lqw = new LambdaQueryWrapper<>();
        lqw.eq(China::getName, province);
        China china = chinaMapper.selectOne(lqw);
        List<China> chinas = chinaMapper.selectList(new LambdaQueryWrapper<China>().eq(China::getPid, china.getId()));
        List<String> citys = new ArrayList<>();
        for(China china1 : chinas) {
            citys.add(china1.getName());
        }
        return citys;
    }

    @Override
    public List<String> getDistricts(String city) {
        LambdaQueryWrapper<China> lqw = new LambdaQueryWrapper<>();
        lqw.eq(China::getName, city);
        China china = chinaMapper.selectOne(lqw);
        List<China> chinas = chinaMapper.selectList(new LambdaQueryWrapper<China>().eq(China::getPid, china.getId()));
        List<String> districts = new ArrayList<>();
        for(China china1 : chinas) {
            districts.add(china1.getName());
        }
        return districts;
    }
}
